﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace AirLibClient
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Main());
        }

        //static void Main(string[] args)
        //{
        //    Main main = new AirPlayClient.Main();
        //    main.Show();         


        //    //Imtiaz.MyWebServer webServer = new Imtiaz.MyWebServer();


        //    //HttpListener httpListener = new HttpListener();
        //    //httpListener.Prefixes.Add("http://*:800/");
        //    // httpListener.Start();
        //    // IAsyncResult result = httpListener.BeginGetContext(new AsyncCallback(ListenerCallback), httpListener);

        //    Play();
        //    //ServerInfo();
        //    Console.ReadLine();

        //}


        //private static void Play()
        //{
        //    string address = "http://192.168.1.20:7000/play";
            
        //    using (WebClient client = new WebClient())
        //    {
        //        client.Headers.Add("Content-Type","text/parameters");
        //        byte[] response = client.UploadValues(address, new NameValueCollection()
        //       {
        //           { "Content-Location", "http://192.168.1.13:800/Video.mov" },
        //           { "Start-Position", "0.1" }
        //       });

        //    }


        //}

        public static void ServerInfo()
        {

            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];
            string address = "http://192.168.1.20:7000/server-info";
            WebRequest webRequest = HttpWebRequest.Create(address);
            var response = webRequest.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?

            // print out page source
            Console.WriteLine(sb.ToString());
           
            

        }

        public static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            // Call EndGetContext to complete the asynchronous operation.
            HttpListenerContext context = listener.EndGetContext(result);
            //HttpListenerRequest request = context.Request;
            // Obtain a response object.
            //HttpListenerResponse response = context.Response;
            
            // Construct a response.

            //Stream stream = response.OutputStream;

            //const int CHUNK_SIZE = 1024;

            string fileName = @"D:\waucissa.mov";
            //var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // Get a response stream and write the response to it.
            //response.ContentType = "application/octet-stream";
            //response.ContentType = "video/mov";
            

            //response.ContentLength64 = fileStream.Length;
            //byte[] buffer = new byte[CHUNK_SIZE];

            //fileStream.Seek(0, SeekOrigin.Begin);
            //for (; ; )
            //{
            //    int count = fileStream.Read(buffer, 0, CHUNK_SIZE);
            //    if (count <= 0)
            //        break;
            //    stream.Write(buffer, 0, count);
            //}
            //stream.Close();

            //response.Close();


            var contentType = GuessContentType(Path.GetExtension(fileName));
            Byte[] content = File.ReadAllBytes(fileName);
            context.Response.StatusCode = (int)HttpStatusCode.OK;
            context.Response.Headers[HttpResponseHeader.ContentType] = contentType;
            context.Response.ContentLength64 = content.Length;
            context.Response.Headers[HttpResponseHeader.AcceptRanges] = "bytes";
            context.Response.Headers.Add("Connection", "close");

            context.Response.OutputStream.Write(content,0,content.Length);

            context.Response.Close();

        }

        

        static string GuessContentType(string ext)
        {
            switch (ext)
            {
                case ".js":
                    return "text/javascript";
                case ".htm":
                case ".html":
                    return "text/html";
                case ".png":
                    return "image/png";
                case ".jpg":
                    return "image/jpg";
                case ".css":
                    return "text/css";
                case ".mov":
                    return "video/quicktime";
                default:
                    return "application/octet-stream";
            }
        }


        private static void Show()
        {
            string address = "http://192.168.1.20:7000/photo";
            var payload = System.IO.File.ReadAllBytes(@"D:\Users\bsykes\Pictures\2012\2012_07_18\20120718_4895.jpg");
            var webClient = new WebClient();


            webClient.Headers.Add("X-Apple-Transition", "None");
            webClient.Headers.Add("User-Agent", "MediaControl/1.0");
            webClient.Headers.Add("X-Apple-Session-ID", Guid.NewGuid().ToString());
            webClient.UploadData(address, payload);

            Console.ReadLine();

        }

        private static void Play()
        {
            string address = "http://192.168.1.199:7000/play";

            using (WebClient client = new WebClient())
            {
                client.Headers.Add("User-Agent", "MediaControl/1.0");
                client.Headers.Add("Content-Type", "text/parameters");


                string response = client.UploadString(address, "Content-Location: http://192.168.1.11//photos/2001/2001_09_24/2001_0924AA.JPG\nStart-Position: 0.0000\n");
                //string response = client.UploadString(address, "Content-Location: http://192.168.1.11:5050/IMG_1227.MOV\nStart-Position: 0.0000\n");
                

                //string response = client.UploadString(address, "Content-Location: http://people.sc.fsu.edu/~jburkardt/data/mov/waucissa.mov\nStart-Position: 0.0000\n");


               

               

            }


        }

    }   
}
